
<script>
  import Modal from './modal.vue'
  import Util from '../util'
  import {EventBus} from '../eventBus'

  export default {
    components: { Modal},
    data() {
      return {
        title: "商品",
        content: "",
        count: 1,
        modal: {
          title: "购物车",
          visible: false,
          okText: "去结算",
          cancelText: "继续购物"
        }
      }
    },
    methods:{
      addToCart(productId) {
        this.modal.visible = !this.modal.visible
        let self = this;
        Util.common_ajax_post_for_html(
          "/carts",
          {
            "product_id": productId,
            "amount": self.count
          },
          function(data) {
            console.log(data)
            self.content = data
          }
        )
      },
      customOkEvent(){
        this.modal.visible = false;
        window.location.href = "/orders/new"
        // you can set modal show or hide with the variable 'this.modal.visible' manually
      },
      customCancelEvent(){
        this.modal.visible = false;
        window.location.href = "/products"
      }
    },
    mounted() {
      let self = this
      EventBus.$on('getProductCount', clickCount => {
        self.count = clickCount
      });
    }
  }
</script>
